﻿using Sustainalytics.Portfolios.BL;
using System;
using System.Collections.Concurrent;

namespace Sustainalytics.Alerts.AlertsEngine
{
    public class PorfolioNamesCache : IPorfolioNamesCache
    {
        private readonly ConcurrentDictionary<Guid, string> _portfolioNames;
        
        public static string AllCompaniesLabel
        {
            get
            {
                return "All companies";
            }
        }
        public PorfolioNamesCache(ConcurrentDictionary<Guid, string> portfolioNames)
        {
            _portfolioNames = portfolioNames;
            _portfolioNames.TryAdd(Guid.Empty, AllCompaniesLabel);
        }

        public string GetPortfolioName(Guid portId, IPortfolioReadHandler portfolioManager)
        {
            string porfolioName;
            if (!_portfolioNames.ContainsKey(portId))
            {
                porfolioName = portfolioManager.GetPortfolioNameById(portId);
            }
            else
            {
                porfolioName = _portfolioNames[portId];
            }
            return porfolioName;
        }

        public void CleanUp()
        {
            _portfolioNames.Clear();
        }
    }
}
